<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Order;
use App\Repositories\CategoryStatisticsRepository;
use App\Repositories\DistributionStatisticsRepository;
use App\Repositories\StatisticsRepository;
use App\User;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
//        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('home');
    }
    public function kefuPage()
    {
        return view('kefu');
    }

    public function miniHint()
    {
        return view('miniHint');
    }

    public function share()
    {
        return view('share');
    }
    public function indexAdvertising()
    {
        return view('index_advertising');
    }

    public function tj()
    {
        set_time_limit(0);
        ini_set ('memory_limit', '2024M');
        $day = request('day',date('Y-m-d'));
        $statistics_repostory = app()->build(StatisticsRepository::class);
        $category_statistics_repostory = app()->build(CategoryStatisticsRepository::class);
        $distribution_statistics_repostory = app()->build(DistributionStatisticsRepository::class);
        $statistics_repostory->generate(1,1,$day);

        //分类统计
        $categories = Category::all();
        foreach ($categories as $category) {
            $category_statistics_repostory->generate($category->id,1,1,$day);
        }

        //配送统计
        $users = User::all();
        foreach ($users as $user) {
            if($user->is_distribution_statistics)
                $distribution_statistics_repostory->generate($user,1,1,$day);
        }

        //订单状态
        $order = Order::where([
            ['status',4],
            ['created_at','<',date('Y-m-d')],
        ])->orWhere([
            ['pairing_id','>',0],
            ['status',4],
            ['created_at','<',date('Y-m-d')],
        ])->update(['status'=>5]);

        Order::where('will_take',1)->update(['shipping_status'=>5]);
    }
}
